<%@ page contentType="text/html; charset=utf-8"%>
<%@page import="com.alibaba.fastjson.JSONObject"%>
<%@page import="java.util.Map"%>
<%@page import="org.apache.commons.lang3.StringUtils"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="manage.commons.tags.AccessControl"%>
<%@page import="manage.commons.Constants"%>
<%@page import="manage.modules.worker.model.WorkerInfo"%>
<%@page import="org.apache.commons.lang3.math.NumberUtils"%>
<%@page import="manage.modules.worker.service.WorkerService"%>
<%@page
	import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	//是否按职能查
	String ability = request.getParameter("ability");
	
	//必填参数
	String organWorkerId = request.getParameter("workerId");
	//这个意义改了，变成特定的部门范围
	String deptRange = request.getParameter("deptRange");		
	//所有数据范围
	String allDataRange = request.getParameter("allDataRange");		
	
	//表单的class属性
	String formClass = request.getParameter("formClass");
	formClass = (formClass == null || formClass.length() < 1) ? "myform": formClass;
	request.setAttribute("formClass",formClass);
	
	//传入参数 分组号，默认值 是1
	String organGroup = request.getParameter("organGroup");
	organGroup = (organGroup == null || organGroup.length() < 1) ? "1": organGroup;
	request.setAttribute("organGroup",organGroup);
	
	//部门标签name属性
	String deptName = request.getParameter("deptName");
	deptName = (deptName == null || deptName.length() < 1) ? "deptId": deptName;
	request.setAttribute("deptName",deptName);
	
	//组别标签name属性
	String groupName = request.getParameter("groupName");
	groupName = (groupName == null || groupName.length() < 1) ? "groupId": groupName;
	request.setAttribute("groupName",groupName);
	
	//人员标签name属性
	String workerName = request.getParameter("workerName");
	workerName = (workerName == null || workerName.length() < 1) ? "workerId": workerName;
	request.setAttribute("workerName",workerName);
	
	//选择框默认值
	String defaultValue = request.getParameter("defaultValue");
	defaultValue =  (defaultValue == null || defaultValue.length() < 1) ? "-1000": defaultValue;
	request.setAttribute("defaultValue",defaultValue);
	
	//标签的其它属性（包括部门、组别、人员）
	String otherAttr = request.getParameter("otherAttr");
	otherAttr =  (otherAttr == null || otherAttr.length() < 1) ? "": otherAttr;
	
	//这个是当读取所有机构树，而且默认选中自己的机构树是使用，使用的时候传1
	String defaultSel = request.getParameter("defaultSel");
	Integer isDefaultSel =  (defaultSel != null && "1".equals(defaultSel)) ? 1 : 0;
	request.setAttribute("isDefaultSel",isDefaultSel);
	
	//不显示人员标签
	String noWorker = request.getParameter("noWorker");
	Integer organNoWorker =  (noWorker != null && "1".equals(noWorker)) ? 1 : 0;
	request.setAttribute("organNoWorker",organNoWorker);
	
	//不显示组别标签
	String noGroup = request.getParameter("noGroup");
	Integer organNoGroup =  (noGroup != null && "1".equals(noGroup)) ? 1 : 0;
	request.setAttribute("organNoGroup",organNoGroup);
	
	//取得基本信息
	WebApplicationContext webctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
	WorkerService workerService = (WorkerService) webctx.getBean("workerService");
	Long workerId  = NumberUtils.toLong(organWorkerId);
	WorkerInfo workerInfo = workerService.selectWorkerInfo(workerId);
	
	Integer oldRank = workerInfo.getRank();
	request.setAttribute("oldRank",oldRank);
	
	
	//这个意义改了，变成特定的部门范围
	int dr = NumberUtils.toInt(deptRange);
	
	//手动设置读取所有部门数据
	if("1".equals(allDataRange)){
		workerInfo.setRank(Constants.DIRECTOR);
	}else{	
		//权限认证相关，动态获取
		String uri = request.getParameter("uri");
		if(StringUtils.isNotBlank(uri)) {
			workerInfo.setRank(NumberUtils.toInt(AccessControl.getRangeValueByWorker(uri,workerId)));
			dr = dr == -1000 ? 0 : dr; 
		}
	}
	
	Map<String, Object> organ = null;
	if(ability == null || ability.length()< 1){
		Integer[] abilityi = new Integer[1];
		organ = workerService.getWorkerOrganization(workerInfo, null);
	}else{
		String[] abilitys = ability.split(",");
		Integer[] abilityi = new Integer[abilitys.length];
		for(int i=0;i<abilitys.length;i++){
			abilityi[i] = NumberUtils.toInt(abilitys[i]);
		}
		organ = workerService.getWorkerOrganization(workerInfo,abilityi);		
	}
	request.setAttribute("organ", organ);		
	request.setAttribute("organJson", JSONObject.toJSON(organ).toString());		
	request.setAttribute("organWorkerInfo", workerInfo);
%>
<style>
	.ideal-form .ideal-wrap{clear: none;}
</style>
<script type="text/javascript">
	var organJson_${organGroup} = {};
	organJson_${organGroup}.gResult = [];
	organJson_${organGroup}.wResult = [];
	try{				
		organJson_${organGroup} = ${organJson};
		var gResult_${organGroup} = organJson_${organGroup}.gResult;
		var wResult_${organGroup} = organJson_${organGroup}.wResult;
	}catch(e){
		alert("初始化部门数据失败...");
	}
	
	$(function(){
		var idealforms=$(".${formClass}").idealforms();
		<c:if test="${organNoGroup ne 1}">
		//部门变的时候
		function dc(){
			idealforms.removeFields('${groupName}');
			var dynamicSelect=[];
			var dynamicCreateGroup={};
			dynamicCreateGroup.name="${groupName}";
			dynamicCreateGroup.label="组别";
			dynamicCreateGroup.type="select";
			dynamicCreateGroup.addAfter="${deptName}";
			var selectKeyValue=[];
			selectKeyValue.push("所有组别:${defaultValue}");
			var cv = $(this).val();
			for(var i = 0, len = gResult_${organGroup}.length; i < len; i++){
				var gi = gResult_${organGroup}[i];
				if(cv == gi.deptId){ 
					var kv=gi.groupName+":"+gi.groupId;
					selectKeyValue.push(kv);
				}
			}
			dynamicCreateGroup.list=selectKeyValue;
			dynamicSelect.push(dynamicCreateGroup);
			idealforms.addFields(dynamicSelect);
			
			<c:if test="${organNoWorker ne 1}">
			var dynamicWorkerSelect=[];
			var dynamicCreateWorker={};
			var selectWorkerKeyValue=[];
			dynamicCreateWorker.type="select";
			dynamicCreateWorker.addAfter="${groupName}";
			dynamicCreateWorker.name="${workerName}";
			dynamicCreateWorker.label="人员";
			var defaultKv="所有人员:${defaultValue}";
			selectWorkerKeyValue.push(defaultKv);
			dynamicCreateWorker.list=selectWorkerKeyValue;
			dynamicWorkerSelect.push(dynamicCreateWorker);
			idealforms.removeFields('${workerName}');
			idealforms.addFields(dynamicWorkerSelect);
			</c:if>	
		}
		$("[name='${deptName}']").live("change",dc);
		</c:if>	
		<c:if test="${organNoWorker ne 1}">
		//组别变的时候
		function gc(){
			var dynamicWorkerSelect=[];
			var dynamicCreateWorker={};
			dynamicCreateWorker.type="select";
			dynamicCreateWorker.addAfter="${groupName}";
			dynamicCreateWorker.name="${workerName}";
			dynamicCreateWorker.label="人员";
			var selectKeyValue=[];
			selectKeyValue.push("所有人员:${defaultValue}");
			
			var cv = $(this).val();
			for(var i = 0, len = wResult_${organGroup}.length; i < len; i++){
				var wi = wResult_${organGroup}[i];
				if(cv == wi.groupId){
					var kv=wi.workerName+":"+wi.workerId;
					selectKeyValue.push(kv);
				}
			}
			dynamicCreateWorker.list=selectKeyValue;
			dynamicWorkerSelect.push(dynamicCreateWorker);	
			idealforms.removeFields('${workerName}');
			idealforms.addFields(dynamicWorkerSelect);
		}	
		$("[name='${groupName}']").live("change",gc);
		</c:if>		
		
		$("[name='${deptName}']").change();
	});
</script>
					<div>
						<label >
							部门:
						</label>
						<select id="${deptName}" name="${deptName}"  class="w3"  <%=otherAttr%>>
							<c:if test="${organ.type ne 4}">
								<c:if test="${organWorkerInfo.depts.size() gt 1 }">
									<option value="${defaultValue}" selected="selected">所有部门</option>
									<c:forEach items="${organWorkerInfo.depts}" var="dept">
										<option value="${dept.deptId}">${dept.deptName}</option>
									</c:forEach>
								</c:if>
								<c:if test="${organWorkerInfo.depts.size() eq 1 }">
										<option value="${organWorkerInfo.depts[0].deptId}">${organWorkerInfo.depts[0].deptName}</option>
								</c:if>
								<c:if test="${organWorkerInfo.depts.size() lt 1 }">
										<option value="${defaultValue}" selected="selected">所有部门</option>
								</c:if>
							</c:if>
							<c:if test="${organ.type eq 4}">
								<option value="${defaultValue}" selected="selected">所有部门</option>
								<c:forEach items="${organ['dResult']}" var="dept">
										<option value="${dept['deptId']}">${dept['deptName']}</option>
								</c:forEach>
							</c:if>
						</select>
					</div>
					<c:if test="${organNoGroup ne 1}">
						<div>
							<label>组别</label>
							<select id="${groupName}" name="${groupName}" class="w2" <%=otherAttr%>>
								<c:if test="${(organ.type ne 4 && organ.type ne 3)}">
									<c:if test="${organWorkerInfo.depts.size() gt 1 }">
										<option value="${defaultValue}" selected="selected">所有组别</option>
									</c:if>
									<c:if test="${organWorkerInfo.depts.size() eq 1 }">
										<c:if test="${organWorkerInfo.groups.size() gt 1 }">
											<option value="${defaultValue}" selected="selected">所有组别</option>
											<c:forEach items="${organWorkerInfo.groups}" var="group">
												<option value="${group.groupId}">${group.groupName}</option>
											</c:forEach>
										</c:if>
										<c:if test="${organWorkerInfo.groups.size() eq 1 }">
											<option value="${organWorkerInfo.groups[0].groupId}">${organWorkerInfo.groups[0].groupName}</option>
										</c:if>
										<c:if test="${organWorkerInfo.groups.size() lt 1 }">
											<option value="${defaultValue}" selected="selected">所有组别</option>
										</c:if>
									</c:if>
									<c:if test="${organWorkerInfo.depts.size() lt 1 }">
										<option value="${defaultValue}" selected="selected">所有组别</option>
									</c:if>
								</c:if>
								<c:if test="${organ.type eq 3}">
									<c:if test="${organWorkerInfo.depts.size() gt 1 }">
										<option value="${defaultValue}" selected="selected">所有组别</option>
									</c:if>
									<c:if test="${organWorkerInfo.depts.size() eq 1 }">
										<c:forEach items="${organ['gResult']}" var="group">
											<option value="${group['groupId']}">${group['groupName']}</option>
										</c:forEach>
									</c:if>
									<c:if test="${organWorkerInfo.depts.size() lt 1}">
										<option value="${defaultValue}" selected="selected">所有组别</option>
									</c:if>
								</c:if>
								<c:if test="${organ.type eq 4}">
									<option value="${defaultValue}" selected="selected">所有组别</option>
								</c:if>
							</select>
						</div>
					</c:if>
					<c:if test="${organNoWorker ne 1}">
					<div>
						<label>人员</label>
						<select id="${workerName}" name="${workerName}" class="w7"  <%=otherAttr%>>
							<c:if test="${(organ.type ne 4 && organ.type ne 3 && organ.type ne 2)}">
								<c:if test="${organWorkerInfo.depts.size() eq 1 && organWorkerInfo.groups.size() eq 1}">
									<option value="${organWorkerInfo.workerId}">${organWorkerInfo.workerName}</option>
								</c:if>
							</c:if>
							<c:if test="${organ.type eq 2}">
								<c:if test="${organWorkerInfo.depts.size() gt 1 }">
									<option value="${defaultValue}" selected="selected">所有人员</option>
								</c:if>
								<c:if test="${organWorkerInfo.depts.size() eq 1 }">
									<c:if test="${organWorkerInfo.groups.size() gt 1 }">
										<option value="${defaultValue}" selected="selected">所有人员</option>
									</c:if>
									<c:if test="${organWorkerInfo.groups.size() eq 1 }">
									<option value="${defaultValue}" selected="selected">所有人员</option>
									<c:forEach items="${organ['wResult']}" var="tworker">
										<option value="${tworker['workerId']}">${tworker['workerName']}</option>
									</c:forEach>
									</c:if>
								</c:if>
							</c:if>
							<c:if test="${organ.type eq 4 || organ.type eq 3}">
								<option value="${defaultValue}" selected="selected">所有人员</option>
							</c:if>
						</select>
					</div>
					</c:if>
